package com.java.reflect;

import java.lang.reflect.Field;

import com.java.reflect.bean.User;


public class GetBeanNames {

	public static void main(String[] args) {
		User user = new User(1,"jing","jing123","");
		Field[] fields = getFields(user);
		for(int i=0; i<fields.length; i++){
			System.out.println("--"+i+"--"+fields[i].getName());
		}
		
	/*	Object[] getObjs = getFieldsVal(user);
		for(int i=0; i<getObjs.length; i++){
			System.out.println(getObjs[i].toString());
		}*/
	}
	
	// ��ȡ������bean������
	public static Field[] getFields(Object obj) {
		return getFields(obj, null);
	}

	@SuppressWarnings("unchecked")
	private static Field[] getFields(Object obj, String[] ss) {
		Field[] fields = null;
		Field[] temp = null;
		Class c = obj.getClass();
		// ��ȡ��������
		temp = c.getDeclaredFields();
		// �ж��Ƿ��в���
		if (ss != null && ss.length > 0) {
			fields = new Field[ss.length];
			for (int i = 0; i < ss.length; i++) {
				for (int j = 0; j < temp.length; j++) {
					if (temp[j].getName().equals(ss[i])) {
						fields[i] = temp[j];
					}
				}
			}
		} else { // ����ȫ������
			fields = temp;
		}
		return fields;
	}

	/*// ��ȡ����bean�е�getXXX
	public static Object[] getFieldsVal(Object bean){
		// ������е�����
		Field[] fields = null;
		fields = bean.getClass().getDeclaredFields();
		String[] ss = new String[fields.length];
		for(int i=0; i<fields.length; i++){
			ss[i] = fields[i].getName();
		}
		
		return getFieldsVal(bean, ss);
	}

	private static Object[] getFieldsVal(Object bean, String[] propertyNames) {
		PropertyDescriptor[] descriptors = null;
		try {
			descriptors = Introspector.getBeanInfo(bean.getClass()).getPropertyDescriptors();
		} catch (IntrospectionException e) {
			e.printStackTrace();
		}
		PropertyDescriptor[] sorted = new PropertyDescriptor[descriptors.length];
		for(int i=0; i<propertyNames.length; ++i){
			String propertyName = propertyNames[i];
			if(propertyName == null){
				throw new NullPointerException("propertyName can't be null: "+i);
			}
			boolean found = false;
			for(int j=0; j<descriptors.length; ++j){
				PropertyDescriptor descriptor = descriptors[j];
				if(propertyName.equals(descriptor.getName()));
				sorted[i] = descriptor;
				found = true;
				break;
			}
			if (!(found))
			 throw new RuntimeException("Couldn't find bean property: " + 
					 bean.getClass() + " " + propertyName);
		}
		return getFieldsVal(bean, sorted);
	}

	private static Object[] getFieldsVal(Object bean, PropertyDescriptor[] properties) {
		Object[] params = new Object[properties.length];
		for(int i=0; i<properties.length-1; ++i){
			PropertyDescriptor property = properties[i];
			Object value = null;
			Method method = property.getReadMethod();
			if(method == null){
				throw new RuntimeException("No read method for bean property"+bean.getClass()
						+" "+property.getName());
			}
			try {
				value = method.invoke(bean, new Object[0]);
			} catch (Exception e) {
				e.printStackTrace();
			} 
			params[i] = value;
		}
		return params;
	}*/
}
